package com.bbm.communitysystem.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.extra.qrcode.QrCodeUtil;
import com.bbm.communitysystem.core.domain.R;
import com.bbm.communitysystem.domain.House;
import com.bbm.communitysystem.domain.dto.HouseDto;
import com.bbm.communitysystem.domain.dto.HouseGuestDto;
import com.bbm.communitysystem.service.IHouseService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Base64;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author bbm
 * @since 2025-02-21
 */
@Tag(name = "房屋")
@RestController
@RequestMapping("/house")
@RequiredArgsConstructor
public class HouseController {

    private final IHouseService iHouseService;

    @Operation(summary = "新增房屋")
    @PostMapping
    public R createHouse(@RequestBody HouseDto houseDto) {
        House house = BeanUtil.copyProperties(houseDto, House.class);
        house.setUpdateTime(System.currentTimeMillis() + "");
        return iHouseService.save(house) ? R.ok("创建成功") : R.error("创建失败");
    }

    @Operation(summary = "获取所有房屋")
    @GetMapping
    public List<House> getAllHouses() {
        return iHouseService.list();
    }

    @Operation(summary = "根据id获取房屋")
    @GetMapping("/{id}")
    public House getHouseById(@PathVariable Long id) {
        return iHouseService.getById(id);
    }

    @Operation(summary = "根据住户id获取房屋")
    @GetMapping("/user/{userId}")
    public R<List<House>> getHousesByUserId(@PathVariable Long userId) {

        List<House> list = iHouseService.lambdaQuery()
                .eq(House::getHouseholdId, userId)
                .list();

        return R.ok(list);
    }

    @Operation(summary = "修改房屋")
    @PutMapping()
    public R updateHouse(@RequestBody HouseDto houseDto) {
        House house = BeanUtil.copyProperties(houseDto, House.class);
        house.setUpdateTime(System.currentTimeMillis() + "");
        return iHouseService.updateById(house) ? R.ok("修改成功") : R.error();
    }

    @Operation(summary = "删除房屋")
    @DeleteMapping("/{id}")
    public boolean deleteHouse(@PathVariable Long id) {
        return iHouseService.removeById(id);
    }


    @Operation(summary = "获取房屋权限")
    @PostMapping("/per")
    public R<String> getHousePer(@Validated @RequestBody HouseGuestDto houseGuest) {


        BufferedImage generate = QrCodeUtil.generate("http://192.168.36.148", 300, 300);

        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

        try {
            ImageIO.write(generate, "png", byteArrayOutputStream);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        byte[] bytes = byteArrayOutputStream.toByteArray();

        return R.ok("获取成功", "data:image/png;base64," + Base64.getEncoder().encodeToString(bytes));
    }

}